<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace app\admin\controller;
use app\admin\model\Depar as Depars;
use think\Db;
/**
 * Description of Depar
 *
 * @author HuangLuSen
 */
class Depar extends Admin {

    /**部门列表
     * @return mixed
     */
    public function index(){
        $authgroup = new Depars();
        $data=$authgroup->order('id desc')->paginate(5);
        $page=$data->render();
        $this->assign( 'datas', $data );
        $this->assign( 'page', $page );
        return $this->fetch();
    }

    /**
     * 添加部门
     * @return mixed
     */
    public function add(){
        return $this->fetch();
    }

    /**
     *保存添加部门
     */
    public function addrun(){
        if(!request()->isAjax()){
            $this->error('提交方式不正确',url('admin/depar/index'));
        }else{
            $authgroup = new Depars();
            $data=input('post.');
            !empty($data['status'])?:$data['status']=0;
            $result=$authgroup->validate(true)->save($data);
            if(false === $result){
                // 验证失败 输出错误信息
                $this->error($authgroup->getError());
            }else{
                $this->success('部门添加成功',url('admin/depar/index'));
            }
        }
    }
    /**
     * 编辑部门
     * @return mixed
     */
    public function edit()
    {
        input('id')?$id = input('id'):$this->error('缺少参数');
        $info= Db::name('Depar')->where(['id'=>$id])->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    /**
     *保存更新部门
     */
    public function editrun()
    {
        if(!request()->isAjax()){
            $this->error('提交方式不正确',url('admin/depar/index'));
        }else{
            $menu = new Depars();
            $data=input('post.');
            !empty($data['status'])?:$data['status']=0;
            $result=$menu->allowField(true)->validate(true)->isUpdate(true)->save($data);
            if($result!==false){
                $this->success('部门更新成功',url('admin/depar/index'));
            }else{
                // 验证失败 输出错误信息
                $this->error($menu->getError());
            }
        }
    }
     //删除部门
     public function del(){
        $id = $this->request->param('id',0);
        if ( empty($id) ) {
            $this->error('请选择要操作的数据!');
        }
        if(Depars::get($id)){
            if(Depars::destroy($id)){
                $this->success('删除成功！');
             } else {
                $this->error('删除失败');
             }
        }else{
            $this->error('要删除的数据不存在！');
        }
    }
}
